﻿
using System.Linq;
using Biz.Buffer;
using Biz.EF.Platform;
using Hjt212Parser;

namespace Biz.Process
{
    /// <summary>
    /// 负责处理响应指令：9011、9012、9014
    /// </summary>
    public static class UplineResponseInstructionBoth
    {
        /// <summary>
        /// 未处理：0；
        /// 正在下行：1；
        /// 下行成功：2；
        /// 下行失败：3；
        /// 执行成功：4；
        /// 执行失败：5.
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process9011(PlatformContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            //阀门控制/提取响应
            var cmdValves = pCtx.反控阀门.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdValves.Count > 0)
            {
                foreach (var cmd in cmdValves)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }
            //设置许可证污染物信息
            var cmdLincens = pCtx.许可证main.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdLincens.Count > 0)
            {
                foreach (var cmd in cmdLincens)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //反控采样
            var cmdSamples = pCtx.反控采样.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSamples.Count > 0)
            {
                foreach (var cmd in cmdSamples)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //留样控制
            var cmdSamples2 = pCtx.留样控制.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSamples2.Count > 0)
            {
                foreach (var cmd in cmdSamples2)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //远程控制门禁
            var cmdGate = pCtx.反控门禁.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdGate.Count > 0)
            {
                foreach (var cmd in cmdGate)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //设置现场机时间
            var cmdTimings = pCtx.现场机系统时间.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdTimings.Count > 0)
            {
                foreach (var cmd in cmdTimings)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //设置做样间隔
            var cmdSampleIntervals = pCtx.设置做样间隔.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSampleIntervals.Count > 0)
            {
                foreach (var cmd in cmdSampleIntervals)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //设置数据上传间隔
            var cmdUpIntervals = pCtx.设置数据上传间隔.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdUpIntervals.Count > 0)
            {
                foreach (var cmd in cmdUpIntervals)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //远程充值
            var cmdChargings = pCtx.远程充值.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdChargings.Count > 0)
            {
                foreach (var cmd in cmdChargings)
                {
                    cmd.执行结果标志ID = 602;
                    pCtx.SaveChanges();
                }
            }

            //限值设定
            var cmdLimits = pCtx.报警设置.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdLimits.Count > 0)
            {
                foreach (var cmd in cmdLimits)
                {
                    cmd.发送结果标志ID = 202;
                    pCtx.SaveChanges();
                }
            }

            var cmdCards = pCtx.IcCards.Where(t => !string.IsNullOrEmpty(t.Qn) && t.Qn.Equals(qn)).ToList();
            if (cmdCards.Count > 0)
            {
                foreach (var cmd in cmdCards)
                {
                    cmd.IsSynchronized = true;
                    //cmd.DisableReason = "已收到";
                    pCtx.SaveChanges();
                }
            }

        }
        public static void Process9012(PlatformContext pCtx, string content)
        {
            var mn = Parser.GetStringByKey("MN", content);
            if (string.IsNullOrEmpty(mn)) return;
            var port = CommonInformation.GetLetportByMn(mn);
            if (port == null) return;
            var qn = Parser.GetStringByKey("QN", content);
            var exeRtn = Parser.GetStringByKey("ExeRtn", content);
            var cmds = pCtx.反控阀门.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmds.Count > 0)
            {
                foreach (var cmd in cmds)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }
            var cmdValves = pCtx.许可证main.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdValves.Count > 0)
            {
                foreach (var cmd in cmdValves)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //反控采样
            var cmdSamples = pCtx.反控采样.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSamples.Count > 0)
            {
                foreach (var cmd in cmdSamples)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //留样控制
            var cmdSamples2 = pCtx.留样控制.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSamples2.Count > 0)
            {
                foreach (var cmd in cmdSamples2)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //远程控制门禁
            var cmdGate = pCtx.反控门禁.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdGate.Count > 0)
            {
                foreach (var cmd in cmdGate)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            var cmdTimings = pCtx.现场机系统时间.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdTimings.Count > 0)
            {
                foreach (var cmd in cmdTimings)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }
            //设置做样间隔
            var cmdSampleIntervals = pCtx.设置做样间隔.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdSampleIntervals.Count > 0)
            {
                foreach (var cmd in cmdSampleIntervals)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //设置数据上传间隔
            var cmdUpIntervals = pCtx.设置数据上传间隔.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdUpIntervals.Count > 0)
            {
                foreach (var cmd in cmdUpIntervals)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //远程充值
            var cmdChargings = pCtx.远程充值.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdChargings.Count > 0)
            {
                foreach (var cmd in cmdChargings)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.执行结果标志ID = 603;
                            break;
                        case "2":
                            cmd.执行结果标志ID = 604;
                            break;
                        case "4":
                            cmd.执行结果标志ID = 602;
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }

            //限值设定
            var cmdLimits = pCtx.报警设置.Where(t => t.企业基本信息ID == port.企业基本信息ID)
                .Where(t => t.设备信息ID == port.ID)
                .Where(t => !string.IsNullOrEmpty(t.QN) && t.QN.Equals(qn)).ToList();
            if (cmdLimits.Count > 0)
            {
                foreach (var cmd in cmdLimits)
                {
                    switch (exeRtn)
                    {
                        case "1":
                            cmd.发送结果标志ID = 203;
                            break;
                        case "2":
                        case "4":
                            cmd.发送结果标志ID = 204;
                            break;
                    }

                    pCtx.SaveChanges();
                }
            }

            var cmdCards = pCtx.IcCards.Where(t => !string.IsNullOrEmpty(t.Qn) && t.Qn.Equals(qn)).ToList();
            if (cmdCards.Count > 0)
            {
                foreach (var cmd in cmdCards)
                {
                    cmd.IsSynchronized = true;
                    switch (exeRtn)
                    {
                        case "1":
                            //cmd.DisableReason = "成功";
                            break;
                        case "2":
                        case "4":
                            //cmd.DisableReason = "失败";
                            break;
                    }
                    pCtx.SaveChanges();
                }
            }
        }
        public static void Process9014(PlatformContext pCtx, string content)
        {
            //TODO:HourDataMain表确定，省略未做
            var qn = Parser.GetStringByKey("QN", content);
            var cmds = pCtx.IcCards.Where(t => !string.IsNullOrEmpty(t.Qn) && t.Qn.Equals(qn)).ToList();
            if (cmds.Count > 0)
            {
                foreach (var cmd in cmds)
                {
                    cmd.IsSynchronized = true;
                    //cmd.DisableReason = "已回复";
                    pCtx.SaveChanges();
                }
            }
        }
    }
}
